using System.Collections.Generic;
using System.Linq;
using QFramework.Framework.Util;
using UnityEditor;
using UnityEngine;

namespace QFramework.Framework.ResKit.Data
{
    public class ResData : Singleton<ResData>
    {
        private static AssetBundleManifest _manifest;
        public readonly List<AssetBundleData> AssetBundleDatas = new();

        private ResData()
        {
            Load();
        }

        private void Load()
        {
            if (ResMgr.IsSimulationModeLogic)
            {
#if UNITY_EDITOR
                // 获取 Asset 下所有的 AssetBundle 名
                var assetBundleNames = AssetDatabase.GetAllAssetBundleNames();

                foreach (var assetBundleName in assetBundleNames)
                {
                    // 获取 AssetBundle 下所有的 资源路径
                    var assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);

                    var assetBundleData = new AssetBundleData
                    {
                        Name = assetBundleName,

                        // 获取 AssetBundle 下所有的 依赖 AssetBundle 名
                        DependencyBundleNames = AssetDatabase.GetAssetBundleDependencies(assetBundleName, false)
                    };

                    foreach (var assetPath in assetPaths)
                    {
                        var assetData = new AssetData
                        {
                            // 通过路径获取名字
                            Name = assetPath.Split('/').Last().Split('.').First(),
                            OwnerBundleName = assetBundleName
                        };

                        assetBundleData.AssetDataList.Add(assetData);
                    }

                    AssetBundleDatas.Add(assetBundleData);
                }

                AssetBundleDatas.ForEach(abData =>
                {
                    Debug.LogFormat("----- {0} -----", abData.Name);

                    abData.AssetDataList.ForEach(assetData =>
                    {
                        Debug.LogFormat("AB:{0} AssetData:{1}", abData.Name, assetData.Name);
                    });

                    foreach (var dependencyBundleName in abData.DependencyBundleNames)
                        Debug.LogFormat("AB:{0} Depend:{1}", abData.Name, dependencyBundleName);
                });
#endif
            }
            else
            {
                var mainBundle =
                    AssetBundle.LoadFromFile(ResKitUtil.FullPathForAssetBundle(ResKitUtil.GetPlatformName()));

                _manifest = mainBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
            }
        }

        public string[] GetDirectDependencies(string bundleName)
        {
            if (ResMgr.IsSimulationModeLogic)
                return Instance.AssetBundleDatas.Find(abData => abData.Name == bundleName).DependencyBundleNames;

            // 获取所有的 AssetBundle 的名字
            // GetAllAssetBundles().ToList().ForEach(assetBundle => { Debug.Log(assetBundle); });

            // 获取目标 AssetBundle 的所有依赖 AssetBundle 的名字（递归遍历）
            // GetAllDependencies(Name).ToList().ForEach(dependency =>{});

            // 获取目标 AssetBundle 的直接依赖 AssetBundle 的名字（只遍历一层）
            return _manifest.GetDirectDependencies(bundleName);
        }
    }
}